package com.dingreading.cloud.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.admin.dto.GoodsGivesDto;
import com.dingreading.cloud.admin.entity.GoodsGives;
import com.dingreading.cloud.admin.entity.table.GoodsGivesTableDef;
import com.dingreading.cloud.admin.mapper.GoodsGivesMapper;
import com.dingreading.cloud.admin.service.GoodsGivesService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.update.UpdateChain;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class GoodsGivesServiceImpl extends BaseServiceImpl<GoodsGivesMapper, GoodsGives, GoodsGivesTableDef> implements GoodsGivesService {

    @Override
    protected GoodsGivesTableDef getTable() {
        return GoodsGivesTableDef.goodsGives;
    }

    @Override
    public PageDto<GoodsGivesDto> goodsPageList(PageUtil pageUtil, String keyword, Integer enabled) {
        Map<String, Object> otherParams = new HashMap<>();
        otherParams.put("keyword", keyword);
        otherParams.put("enabled", enabled);

        Page<GoodsGivesDto> page = getMapper().xmlPaginate("goodsPageList", Page.of(pageUtil.getPage(), pageUtil.getRows()), otherParams);

        long count = page.getTotalRow();
        List<GoodsGivesDto> list = page.getRecords();

        PageDto<GoodsGivesDto> dto = new PageDto<>();
        dto.setTotal(count);
        dto.setList(list);
        return dto;
    }

    @Override
    public PageDto<JSONObject> couponPageList(PageUtil pageUtil, String keyword, Integer enabled) {
        Map<String, Object> otherParams = new HashMap<>();
        otherParams.put("keyword", keyword);
        otherParams.put("enabled", enabled);

        Page<JSONObject> page = getMapper().xmlPaginate("couponPageList", Page.of(pageUtil.getPage(), pageUtil.getRows()), otherParams);

        long count = page.getTotalRow();
        List<JSONObject> list = page.getRecords();

        PageDto<JSONObject> data = new PageDto<>();
        data.setTotal(count);
        data.setList(list);
        return data;
    }

    @Override
    public GoodsGives getById(Long id) {
        return getMapper().selectOneByCondition(table.id.eq(id));
    }

    @Override
    public int dropBean(Long id) {
        return getMapper().deleteByCondition(table.id.eq(id));
    }

    @Override
    public boolean haveByProductId(Integer type, Long productId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.type.eq(type))
                .and(table.productId.eq(productId));

        return exists(condition);
    }

    @Override
    public boolean haveGoodsId(Long goodsId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.type.eq(2))
                .and(table.goodsId.eq(goodsId));

        return exists(condition);
    }

    @Override
    public boolean updateEnabled(Long id, String staffUid, String staffName, Integer enabled) {
        return UpdateChain.of(GoodsGives.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<GoodsGives> getByProductIdsAndType(List<Long> ids, Integer type) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productId.in(ids))
                .and(table.type.eq(type));
        return getMapper().selectListByCondition(condition);
    }

}
